/*
 * Copyright 2013 HVTTGROUP
 * Tool Genneric code by hvttgroup
 * @version v1.0
 * @see http://hvttgroup.com
 *
 */
package com.hvtt.web.models;

import com.hvtt.web.connection.Command;
import com.hvtt.web.connection.WEBConnection;
import com.hvtt.web.connection.WEBParamCollection;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

/**
 *
 * @author Nguyen Hoang Danh
 */
public class Customers implements Serializable{

// <editor-fold defaultstate="collapsed" desc="Generic code properties for class">
    private String customerID = "";
    private String customerName = "";
    private String productName = "";
    private String images = "";
    private String address = "";
    private String homePhone = "";
    private String cellPhone = "";
    private String email = "";
    private String website = "";
    private int order = 0;
    private String otherInfo = "";
    private String description = "";
    private WEBConnection webConnection;
    private static String STORE_NAME = "{call proc_customers(?,?,?,?,?,?,?,?,?,?,?,?,?)}";
    private static String TABLE_NAME = "customers";
    private static final String key = "CustomerID";
//</editor-fold>

// <editor-fold defaultstate="collapsed" desc="Generic code Constructor class">
    public Customers() {
        webConnection = WEBConnection.getInstance();
    }

    public Customers(String customerID, String customerName, String productName, String images, String address, String homePhone, String cellPhone, String email, String website, int order, String otherInfo, String description) {
        this.customerID = customerID;
        this.customerName = customerName;
        this.productName = productName;
        this.images = images;
        this.address = address;
        this.homePhone = homePhone;
        this.cellPhone = cellPhone;
        this.email = email;
        this.website = website;
        this.order = order;
        this.otherInfo = otherInfo;
        this.description = description;
        webConnection = WEBConnection.getInstance();
    }

//</editor-fold>
// <editor-fold defaultstate="collapsed" desc="Generic code init param">
    private WEBParamCollection fillParamsAndAction(String action) {
        WEBParamCollection params = WEBParamCollection.getInstance();
        params.add(action);
        params.add(this.getCustomerID());
        params.add(this.getCustomerName());
        params.add(this.getProductName());
        params.add(this.getImages());
        params.add(this.getAddress());
        params.add(this.getHomePhone());
        params.add(this.getCellPhone());
        params.add(this.getEmail());
        params.add(this.getWebsite());
        params.add(this.getOrder());
        params.add(this.getOtherInfo());
        params.add(this.getDescription());
        return params;
    }

    private WEBParamCollection fillParamsAndAction(String action, Customers customers1) {
        WEBParamCollection params = WEBParamCollection.getInstance();
        params.add(action);
        params.add(customers1.getCustomerID());
        params.add(customers1.getCustomerName());
        params.add(customers1.getProductName());
        params.add(customers1.getImages());
        params.add(customers1.getAddress());
        params.add(customers1.getHomePhone());
        params.add(customers1.getCellPhone());
        params.add(customers1.getEmail());
        params.add(customers1.getWebsite());
        params.add(customers1.getOrder());
        params.add(customers1.getOtherInfo());
        params.add(customers1.getDescription());
        return params;
    }

// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Generic code event">
    public boolean insert(Customers varCustomers) {
        boolean bExcute = webConnection.executeUpdate(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("Add", varCustomers));
        return bExcute;
    }

    public boolean save() {
        boolean bExcute = webConnection.executeUpdate(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("Add"));
        return bExcute;
    }

    public boolean update(Customers varCustomers) {
        boolean bExcute = webConnection.executeUpdate(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("Update", varCustomers));
        return bExcute;
    }

    public boolean update() {
        boolean bExcute = webConnection.executeUpdate(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("Update"));
        return bExcute;
    }

    public boolean remove(String key) {
        this.setCustomerID(key);
        boolean bExcute = webConnection.executeUpdate(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("Delete"));
        return bExcute;
    }

    public boolean remove(List<Customers> listCustomers) {
        WEBParamCollection p = WEBParamCollection.getInstance();
        boolean result = false;
        String sql = "delete from " + TABLE_NAME + " where " + TABLE_NAME + "." + key + " in (";
        for (int i = 0; i < listCustomers.size(); i++) {
            sql = sql + "?,";
            p.add(listCustomers.get(i).getCustomerID());
            if (i % 900 == 0) {
                sql = sql.substring(0, sql.lastIndexOf(",")) + ")";
                result = webConnection.executeUpdate(sql, Command.sqlText, p);
                p.clear();
                sql = "delete from " + TABLE_NAME + " where " + TABLE_NAME + "." + key + " in (";
            }
        }
        if (sql.lastIndexOf(",") != -1) {
            sql = sql.substring(0, sql.lastIndexOf(",")) + ")";
            result = webConnection.executeUpdate(sql, Command.sqlText, p);
        }
        return result;
    }

    public boolean removeAll() {
        WEBParamCollection p = WEBParamCollection.getInstance();
        boolean bExcute = webConnection.executeUpdate("delete from " + TABLE_NAME, Command.sqlText, p);
        return bExcute;
    }

    public Customers findByKey(String key) {
        Customers varCustomers1 = null;

        try {
            this.setCustomerID(key);

            List<Customers> result = webConnection.executeQuery(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("GetByKey"), Customers.class);
            if (!result.isEmpty()) {
                varCustomers1 = result.get(0);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Customers.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
        return varCustomers1;
    }

    public List<Customers> findAll() {
        List<Customers> lstCustomers = null;
        try {
            lstCustomers = webConnection.executeQuery(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("GetAll"), Customers.class);
        } catch (SQLException ex) {
            Logger.getLogger(Customers.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
        return lstCustomers == null ? new ArrayList<Customers>() : lstCustomers;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Generic code getter and setter for properties of class">
    public String getCustomerID() {
        return customerID;
    }

    public String getCustomerName() {
        return customerName;
    }

    public String getProductName() {
        return productName;
    }

    public String getImages() {
        return images;
    }

    public String getAddress() {
        return address;
    }

    public String getHomePhone() {
        return homePhone;
    }

    public String getCellPhone() {
        return cellPhone;
    }

    public String getEmail() {
        return email;
    }

    public String getWebsite() {
        return website;
    }

    public int getOrder() {
        return order;
    }

    public String getOtherInfo() {
        return otherInfo;
    }

    public String getDescription() {
        return description;
    }

    public void setCustomerID(String customerID) {
        this.customerID = customerID;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public void setImages(String images) {
        this.images = images;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setHomePhone(String homePhone) {
        this.homePhone = homePhone;
    }

    public void setCellPhone(String cellPhone) {
        this.cellPhone = cellPhone;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    public void setOrder(int order) {
        this.order = order;
    }

    public void setOtherInfo(String otherInfo) {
        this.otherInfo = otherInfo;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void resetAttribute() {
        customerID = "";
        customerName = "";
        productName = "";
        images = "";
        address = "";
        homePhone = "";
        cellPhone = "";
        email = "";
        website = "";
        order = 0;
        otherInfo = "";
        description = "";
    }

    public void setWebConnection(WEBConnection webConnection) {
        this.webConnection = webConnection;
    }

    public WEBConnection getWebConnection() {
        return webConnection;
    }
    public String getUrlImage(String imageFolder) {
        if (StringUtils.isBlank(this.getImages())) {
            return "/images/Customer/default-customer-image.jpg";
        } else {
            return imageFolder + "/" + this.getImages();
        }
    }
//</editor-fold>
// <editor-fold defaultstate="collapsed" desc="Generic code hasCode and equals method">
    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(this.customerID).append(this.customerName).toHashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj instanceof Customers) {
            Customers otherObj = (Customers) obj;
            return new EqualsBuilder().append(this.customerID, otherObj.customerID).append(this.customerName, otherObj.customerName).isEquals();
        }
        return false;
    }

//</editor-fold>
// <editor-fold defaultstate="collapsed" desc="Generic code filterFields">
    public static Field[] filterFields() {
        try {
            Class c = Customers.class;
            return new Field[]{c.getDeclaredField("customerID"), c.getDeclaredField("customerName"), c.getDeclaredField("productName"), c.getDeclaredField("images"), c.getDeclaredField("address"), c.getDeclaredField("homePhone"), c.getDeclaredField("cellPhone"), c.getDeclaredField("email"), c.getDeclaredField("website"), c.getDeclaredField("order")};
        } catch (NoSuchFieldException | SecurityException ex) {
            Logger.getLogger(Customers.class.getName()).log(Level.SEVERE, null, ex);
        }
        return new Field[]{};
    }

//</editor-fold>
// <editor-fold defaultstate="collapsed" desc="Generic code other method">
    public Customers newInstance() {
        try {
            return this.getClass().newInstance();
        } catch (InstantiationException | IllegalAccessException ex) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
        }
        return new Customers();
    }

    /**
     * Copy properties form Customers to this
     *
     * @param customers1
     * @return Customers
     */
    public Customers newInstance(Customers customers1) {
        this.setCustomerID(customers1.getCustomerID());
        this.setCustomerName(customers1.getCustomerName());
        this.setProductName(customers1.getProductName());
        this.setImages(customers1.getImages());
        this.setAddress(customers1.getAddress());
        this.setHomePhone(customers1.getHomePhone());
        this.setCellPhone(customers1.getCellPhone());
        this.setEmail(customers1.getEmail());
        this.setWebsite(customers1.getWebsite());
        this.setOrder(customers1.getOrder());
        this.setOtherInfo(customers1.getOtherInfo());
        this.setDescription(customers1.getDescription());
        return this;
    }
//</editor-fold>
}
